import random
from common.response import Response, response2dict
from models import User, db, Borrow, Book
from util.jsonUtil import user2dict, book2dict


def getAllUserList():
    users = User.query.all()
    code = "200"
    msg = "查询成功"
    data = [user2dict(user) for user in users]
    response = Response(code, msg, data)
    return response2dict(response)


def getOneUser(userId):
    user = User.query.get(userId)
    code = "200"
    msg = "查询成功"
    data = user2dict(user)
    response = Response(code, msg, data)
    return response2dict(response)


def login(username, password):
    user = User.query.filter_by(username=username).first()
    print(user)
    if user is None:
        code = "404"
        msg = "用户名错误"
        data = username
        response = Response(code, msg, data)
        return response2dict(response)
    elif user.password != password:
        code = "404"
        msg = "密码错误"
        data = password
        response = Response(code, msg, data)
        return response2dict(response)
    else:
        code = "200"
        msg = "登陆成功"
        data = user2dict(user)
        response = Response(code, msg, data)
        return response2dict(response)


def register(user0):
    print(user0.username)
    user = User.query.filter_by(username=user0.username).first()
    if user is None:
        user1 = User.query.order_by(User.id.desc()).first()
        print(user1)
        user0.id = user1.id + 1
        db.session.add(user0)
        db.session.commit()
        code = "200"
        msg = "注册成功"
        data = user2dict(user0)
        response = Response(code, msg, data)
        return response2dict(response)
    print(user0)
    code = "404"
    msg = "用户已存在"
    data = user2dict(user0)
    response = Response(code, msg, data)
    return response2dict(response)


def updateUser(id, user0):
    user0.id = id
    users = User.query.filter_by(username=user0.username).all()
    for user in users:
        print(user.id)
        if int(user.id) != int(id):
            code = "404"
            msg = "用户名已存在"
            data = user2dict(user0)
            response = Response(code, msg, data)
            return response2dict(response)
    user = User.query.get(id)
    user.username = user0.username
    user.password = user0.password
    user.power = user0.power
    user.picture = user0.picture
    db.session.commit()
    code = "200"
    msg = "更新成功"
    data = user2dict(user0)
    response = Response(code, msg, data)
    return response2dict(response)



def deleteUser(id):
    user = User.query.get(id)
    db.session.delete(user)
    db.session.commit()
    code = "200"
    msg = "删除成功"
    data = user2dict(user)
    response = Response(code, msg, data)
    return response2dict(response)


def recommendUser(id):
    borrowers = Borrow.query.filter_by(user_id=id).all()
    num_list = [0, 0, 0, 0, 0]
    b = "科学"
    lei_list = ["生命", "生活", "科学", "生物", "历史"]
    for borrower in borrowers:
        book = Book.query.filter_by(id=borrower.book_id).first()
        print(book.sort)
        text_list = book.sort.split()
        print(text_list)
        for text in text_list:
            if text == "生命":
                num_list[0] += 1
            if text == "生活":
                num_list[1] += 1
            if text == "科学":
                num_list[2] += 1
            if text == "生物":
                num_list[3] += 1
            if text == "历史":
                num_list[4] += 1
    print(num_list)
    a = num_list[0]
    c = -1
    for num in num_list:
        c += 1
        if num > a:
            a = num
            b = lei_list[c]

    books = Book.query.all()
    book_list = []
    print(b)
    for book in books:
        if b in book.sort:
            book_list.append(book)
    random.seed()
    random.shuffle(book_list)
    book0 = book_list[0]
    code = "200"
    msg = "推荐成功"
    data = book2dict(book0)
    response = Response(code, msg, data)
    return response2dict(response)
